package com.sx.pipeline.ui.wallet.withdraw

import android.app.Application
import android.text.TextUtils
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.Logs
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.R
import com.sx.pipeline.app.config.WithDrawType
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.AddBankDTO
import com.sx.pipeline.repos.http.req.WithdrawDTO
import com.sx.pipeline.repos.http.res.*
import com.sx.pipeline.utils.AndroidUtil

/**
 *    author : zhouyouqiang
 *    e-mail : 1638473898@qq.com
 *    date   : 2022/5/20 13:51
 *    desc   :
 *    version: 1.0
 */
class WithdrawVM(app: Application) : CYBaseViewModel(app) {

    /**
     * 获取提现明细标题
     */
    fun getWithdrawTabsList(): List<String> {
        val tabs = mutableListOf<String>()
        tabs.add(getApplication<Application>().getString(R.string.txt_under_review))
        tabs.add(getApplication<Application>().getString(R.string.txt_passed))
        tabs.add(getApplication<Application>().getString(R.string.txt_rejected))
        return tabs
    }

    /**
     * 获取提现明细标题id
     */
    fun getWithdrawTabsId(): List<Int> {
        val tabs = mutableListOf<Int>()
        tabs.add(WithDrawType.PENDING_REVIEW.code)
        tabs.add(WithDrawType.TO_BE_INQUIRED.code)
        tabs.add(WithDrawType.REJECTED.code)
        return tabs
    }

    /**
     * 获取银行卡列表
     */
    // 银行卡列表
    val bankStringData: MutableList<String> = ArrayList()
    val bankListEvent: SingleLiveEvent<Ipage<BankListVO>> = SingleLiveEvent()
    fun getBankCardList(page: Int = 1) {
        addSubscribe(
            mModel.getBankCardList(page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<BankListVO>, BaseViewModel<*>>
                    (this, false) {
                    override fun onResult(result: Ipage<BankListVO>) {
                        if (bankStringData.size != 0) {
                            bankStringData.clear()
                        }
                        for (data in result.records) {
                            bankStringData?.add(data.bank+"("+AndroidUtil.hideCardNo(data.bankAccount)+")")
                        }
                        bankListEvent.postValue(result)
                        mUI.initRefreshEvent().postValue(true)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                        mUI.initRefreshEvent().postValue(false)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }
                })
        )
    }


    /**
     * 提现
     */
    val withdrawSuccessEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()
    fun getWithdraw(
        bankId: Int,
        payword: String,
        paymoney: String,
    ) {

        val dto = WithdrawDTO(bankId,payword,paymoney)
        addSubscribe(
            mModel.getWithdraw(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        withdrawSuccessEvent.postValue(true)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 获取提现记录列表
     */
    val withRecordListEvent: SingleLiveEvent<Ipage<WithdrawRecordVO>> = SingleLiveEvent()
    fun findWithdrawByPage(status:Int,page: Int = 1) {
        addSubscribe(
            mModel.findWithdrawByPage(status,page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<WithdrawRecordVO>, BaseViewModel<*>>
                    (this, false) {
                    override fun onResult(result: Ipage<WithdrawRecordVO>) {
                        withRecordListEvent.postValue(result)
                        mUI.initRefreshEvent().postValue(true)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                        mUI.initRefreshEvent().postValue(false)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }
                })
        )
    }
}